home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* polarView.c - show how to combine modeling transformations to
- * view objects as though they are encased in a glass ball
- *
- * F1 key - print help information
- * Left Arrow Key - increment the azimuth angle
- * Right Arrow Key - decrement the azimuth angle
- * Up Arrow Key - increment the incidence angle
- * Down Arrow Key - decrement the incidence angle
- * R Key - reset viewpoint
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <math.h>
- #include <stdio.h> /* for printf */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static char *progname;
-
- static GLfloat beamWidth = 2.0, beamHeight = 0.4, beamDepth = 1.0;
- static GLfloat near, far, distance, twistAngle, incAngle, azimAngle;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( (width / 2) + 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- progname = argv[0];
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - combine modeling transformations to \n"
- "view objects as though encased in a glass ball\n\n"
- "Axes: X - red, Y - green, Z - blue\n\n"
- "F1 Key - print Help information\n"
- "Left Arrow Key - increment the azimuth angle\n"
- "Right Arrow Key - decrement the azimuth angle\n"
- "Up Arrow Key - increment the incidence angle\n"
- "Down Arrow Key - decrement the incidence angle\n"
- "<R> Key - reset viewpoint\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat maxObjectSize;
-
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glShadeModel( GL_FLAT );
- glEnable( GL_DEPTH_TEST );
-
- /* Maximum size of all the objects in your scene */
- maxObjectSize = fsqrt( ((2*beamWidth) * (2*beamWidth)) +
- ((2*beamHeight) * (2*beamHeight)) +
- (beamDepth * beamDepth) );
-
- /* Set up near and far so that ( far - near ) > maxObjectSize, */
- /* and determine the viewing distance (adjust for zooming) */
- near = 1.0;
- far = near + 8*maxObjectSize;
- resetView();
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, near, far );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_F1: /* print Help */
- printHelp( progname );
- break;
-
- case GLUT_KEY_LEFT:
- azimAngle = fmodf( (azimAngle + 5.0), 360.0 );
- glutPostRedisplay();
- break;
-
- case GLUT_KEY_RIGHT:
- azimAngle = fmodf( (azimAngle - 5.0), 360.0 );
- glutPostRedisplay();
- break;
-
- case GLUT_KEY_UP:
- incAngle = fmodf( (incAngle + 5.0), 360.0 );
- glutPostRedisplay();
- break;
-
- case GLUT_KEY_DOWN:
- incAngle = fmodf( (incAngle - 5.0), 360.0 );
- glutPostRedisplay();
- break;
- }
- }
-
- void
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static GLfloat upperArmColor[] = { 1.0, 0.0, 0.0 };
- static GLfloat lowerArmColor[] = { 0.8, 0.5, 0.5 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- polarView( distance, azimAngle, incAngle, twistAngle );
- XYZaxes();
-
- /* Draw the shoulder centered at the new origin */
- glTranslatef( 1.0, 0.0, 0.0 );
- glColor3fv( upperArmColor );
- WireBox( beamWidth, beamHeight, beamDepth );
-
- /* Draw the lower arm at the end of the upper arm and
- * rotate it 45 degrees */
- glTranslatef( 1.0, 0.0, 0.0 );
- glRotatef( 45.0, 0.0, 0.0, 1.0 );
- glTranslatef( 1.0, 0.0, 0.0 );
- glColor3fv( lowerArmColor );
- WireBox( beamWidth, beamHeight, beamDepth );
- glPopMatrix();
-
- glutSwapBuffers();
- }
-